package com.example.fashionshoes.controller.user;

import com.example.fashionshoes.exception.BusinessException;
import com.example.fashionshoes.pojo.po.Result;
import com.example.fashionshoes.pojo.po.ShoppingCart;
import com.example.fashionshoes.service.CartService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Resource
    private CartService cartService;

    /**
     * 新增购物车数据
     * @param shoppingCart 购物车数据
     * @return 购物车数据
     */
    @PostMapping("/add")
    public Result<ShoppingCart> insert(@RequestBody ShoppingCart shoppingCart){
        try {
            cartService.insert(shoppingCart);
        } catch (BusinessException e) {
            return Result.error("库存不足！！！");
        }
        return Result.success();
    }

    /**
     * 根据用户id查找所有购物车数据
     * @param userId 用户id
     * @return 所有购物车数据
     */
    @GetMapping
    public Result<List<ShoppingCart>> getByUserId(@RequestParam("userId") Integer userId){
        List<ShoppingCart> shoppingCartList = cartService.getByUserId(userId);
        return Result.success(shoppingCartList);
    }

    /**
     * 根据skuId移除购物车数据
     * @param skuId skuId
     * @return Null
     */
    @DeleteMapping
    public Result<Object> remove(@RequestParam("skuId") String skuId){
        cartService.removeCart(skuId);
        return Result.success();
    }

    /**
     * 更新购物车数据
     * @param shoppingCart 购物车相关数据
     * @return Null
     */
    @PostMapping("/update")
    public Result<Object> update(@RequestBody ShoppingCart shoppingCart){
        cartService.updateCart(shoppingCart);
        return Result.success();
    }
}
